app.controller('xiangqing', function ($scope, $http) {
    //页面控制js
    $scope.buyNow = function () {
        var show = document.getElementsByClassName('alert-all')[0];
        show.style.display = 'block';


    };
    $scope.buyNo = function () {
        var show = document.getElementsByClassName('alert-all')[0];
        show.style.display = 'none';


    };
    var classId = sessionStorage.getItem('classId');
    var token = localStorage.getItem('token');
    $scope.classId = classId;
    $scope.collectionInfo = {};
    $scope.orderInfo = {};
    if (!classId) {
        $.toast('找不到课程');
        location.href = '#homenewest';

        return;
    }

    //得到课程信息
    $scope.getClassInfo = function () {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        $('.xiangqing-content').hide();
        $http({
            method: 'get',
            url: app.netPath + '/class/aclassinfo',
            params: {classId: classId}

        }).then(function (res) {
            // 删除加载提示符
            infiniteScrollPreloader.hide();
            if (!res.data.error) {
                $('.xiangqing-content').show();
                console.log(res.data.data);
                $scope.classInfo = res.data.data;
                for (var i = 0; i < $scope.classInfo.classEvaluateGrade; i++) {
                    $("#grade").before("<span class='fa fa-star'></span>");
                }
            }

            else {
                $.toast(res.data.msg)
            }
        }, function () {
            $.hidePreloader();
            $.toast('网络走丢了！')
        })
    };

//加入收藏夹

    $scope.addCollection = function () {
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }

        if (token) {
            var collectionInfo = {'scClassId': classId, 'scType': 0};
            $http({
                method: 'post',
                url: app.netPath + '/sc/add',
                headers: {Authorization: token},
                data: collectionInfo
            }).then(function (res) {
                if (!res.data.error) {
                    $.toast("添加收藏成功")
                }
                else {
                    $.toast(res.data.msg)

                }
            });
        }
    };

    //加入购物车

    $scope.addShoppingCar = function () {
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }

        if (token) {
            var collectionInfo = {'scClassId': classId, 'scType': 1};
            $http({
                method: 'post',
                url: app.netPath + '/sc/add',
                headers: {Authorization: token},
                data: collectionInfo
            }).then(function (res) {
                if (!res.data.error) {
                    $.toast("添加购物车成功")
                }
                else {
                    $.toast(res.data.msg)

                }
            });
        }
    };

    //生成订单

    $scope.addOrder = function () {
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }

        if (token) {

            $http({
                method: 'get',
                url: app.netPath + '/order/add',
                headers: {Authorization: token},
                params: {classId: classId}
            }).then(function (res) {
                console.log(res.data);
                if (!res.data.error) {
                    //查看订单
                    $scope.buyNow();
                    $http({
                        method: 'get',
                        url: app.netPath + '/order/show/noworderinfo',
                        headers: {Authorization: token}
                    }).then(function (res) {
                        if (!res.data.error) {
                            console.log(res.data);
                            $scope.orderInfo = res.data.data;
                            sessionStorage.setItem('orderId', $scope.orderInfo.orderId)
                        } else {

                            $scope.buyNo();
                        }
                    });
                } else if (res.data.error == 102) {
                    //存在该订单，查询订单详情
                    $scope.buyNow();
                    $http({
                        method: 'get',
                        url: app.netPath + '/order/show/aclassorderinfo',
                        headers: {Authorization: token},
                        params: {classId: classId}
                    }).then(function (res) {
                        if (!res.data.error) {
                            console.log(res.data);
                            $scope.orderInfo = res.data.data;
                            sessionStorage.setItem('orderId', $scope.orderInfo.orderId)
                        } else {

                            $scope.buyNo();
                        }
                    })
                }
                else {
                    $.toast(res.data.msg);
                    $scope.buyNo();

                }

            })
        }
    };

    //购买课程


    $scope.buyClass = function () {
        $.confirm("是否购买此课程？", "", function () {

            if (!token) {
                $.toast("请先登录");
                location.href = '#user-login';

                return;
            }

            if (token) {


                var orderId = sessionStorage.getItem('orderId');
                $http({
                    method: 'get',
                    url: app.netPath + '/order/payorder',
                    headers: {Authorization: token},
                    params: {orderId: orderId}
                }).then(function (res) {
                    if (!res.data.error) {
                        console.log(res.data + ">>>");
                        $.toast("支付成功");
                        $scope.buyNo();
                    }
                    else {
                        $.toast(res.data.msg);
                    }

                })

            }
        }, function () {
            $.toast("交易取消")
        });


    };

    $scope.studyNow = function () {
        location.href = '#linetake'
    }

});
